﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using LinkShareWpf.Commands;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Markup;
using System.Collections.ObjectModel;
using UniCT.LinkShareWpf.Interface;
using System.Collections.Specialized;
using System.Windows.Data;
using System.Windows.Media.Imaging;
using System.IO;
using System.Reflection;
using UniCT.LinkShareWpf.CommunicationChannel;
using LinkShareWpf.Properties;
using UniCT.LinkShareWpf.Model;


namespace LinkShareWpf.ViewModel
{

    public partial class InsertResourceViewModel
    {

        #region Events
        public event EventHandler CloseWindowRequested;
        #endregion

        #region Private
        private bool isTVSeries;
        private Resource res;
        #endregion
        
        #region Commmands
        public DelegateCommand CheckedCommand { get; private set; }
        public DelegateCommand UnCheckedCommand { get; private set; }
        public DelegateCommand InsertCommand { get; private set; }
        public DelegateCommand UndoCommand { get; private set; }
        public DelegateCommand AddActorCommand { get; private set; }
        public DelegateCommand RemoveActorCommand { get; private set; }
        #endregion

        #region Properties
        public Resource Resource
        {
            get { return res; }
            set { res = value; }
        }
        #endregion

        public InsertResourceViewModel()
        {
            Console.WriteLine("InsertResourceViewModel");
            res = new Resource();
            isTVSeries = false;
            CheckedCommand = new DelegateCommand(o => CheckedTVSerie());
            UnCheckedCommand = new DelegateCommand(o => UnCheckedTVSerie());
            InsertCommand = new DelegateCommand(o => Insert(o));
            UndoCommand = new DelegateCommand(o => Undo());
            AddActorCommand= new DelegateCommand(o => AddActor(o));
            RemoveActorCommand = new DelegateCommand(o => RemoveActor(o));
        }

        private void CheckedTVSerie()
        {
            Console.WriteLine("CheckedTVSerie");
            isTVSeries = true;
            Console.WriteLine("isTVSeries = " + isTVSeries);
        }

        private void UnCheckedTVSerie()
        {
            Console.WriteLine("UnCheckedTVSerie");
            isTVSeries = false;
            Console.WriteLine("isTVSeries = " + isTVSeries);
        }

        private void AddActor(object actorBox)
        {
            TextBox tb = actorBox as TextBox;
            string act = tb.Text;
            if (!res.CInfo.Cast.Contains(act) && !act.Equals(string.Empty))
                res.CInfo.Cast.Add(act);
            tb.Text = string.Empty;
        }

        private void RemoveActor(object actor)
        {
            string act = actor as string;
            res.CInfo.Cast.Remove(act);
        }
        //La risorsa è formata da un ContentInfo+TecnicalInfo, o ContentInfoTVS+TecnicalInfo. 
        //ContentInfoTVS eredita da ContentInfo quindi ha tutti i parametri del padre più quelli del figlio. 
        //Leggo CInfo (parametri Film) e creo una copia che chiamo cInfoTVS alla quale aggiungo i parametri della serieTV(titolo, serie..) e aggiorno CInfo con cInfoTVS(parametri SerieTV)
        private void Insert(object commandParameter)
        {
            ContentInfoTVS cInfoParameter = commandParameter as ContentInfoTVS;
            if (isTVSeries)
            {
                Console.WriteLine("TVSERIES");
                ContentInfoTVS cInfoTVS = new ContentInfoTVS(res.CInfo);
                cInfoTVS.EpisodTitle = cInfoParameter.EpisodTitle;
                cInfoTVS.SeasonNumber = cInfoParameter.SeasonNumber;
                cInfoTVS.EpisodNumber = cInfoParameter.EpisodNumber;
                res.CInfo = cInfoTVS; // sostituisco al vecchio Cinfo il nuovo cInfoTVS appena creato
            }
            else
            {
                Console.WriteLine("FILM");
            }
            MyResources.Instance.Resources.Add(res);
            UpdateCoverName();
            MyResources.Instance.Save();
            this.CloseWindowRequested(this, EventArgs.Empty);
            WindowHandler.Instance.UpdateRequest();
        }

        private void Undo()
        {
            this.CloseWindowRequested(this, EventArgs.Empty);
        }

        public void UpdateCoverName()
        {
            try
            {
                Uri uri = new Uri(res.CInfo.CoverName);//si crea un URI a partire dal nome dell'immagine
                string coverName = uri.LocalPath;//estrae il percorso del file
                String uniqueCoverName = Guid.NewGuid().ToString() + ".jpg";
                String photolocation = Settings.Default.CoversPath + "\\" + uniqueCoverName;//percorso dove salvare l'immagine (lo vedo nei Settings)
                File.Copy(coverName, photolocation); //copia coverName(percorso del pc) in photolocation (percorso assoluto)
                res.CInfo.CoverName = uniqueCoverName;
            }
            catch (Exception e)
            {
                Console.WriteLine("*** Eccezione generata da InsertResourceViewModel.UpdateCoverName() ***\n" + e.Message+ "\n" + e.InnerException);
            }
        }

       

 

  

      

    }

}

